import axios from 'axios';
global.constants = {
    LOCALHOST:'http://47.104.240.208:8090/',    //大数网址
    GOLDEYELOCALHOST:'http://47.104.240.208/', //测试网址
    GOLDEYEGETHOST:'http://47.104.240.208/',   //测试接口
    // LOCALHOST:'https://eye.cfbond.com/',    //大数网址
    // GOLDEYELOCALHOST:'https://gdpage.cfbond.com/', //网址
    // GOLDEYEGETHOST:'https://goldeye.cfbond.com/',   //接口
    // LOCALHOST:'https://eye.cfbond.com/',    //大数网址
    // GOLDEYELOCALHOST:'https://gdpage.cfbond.com/', //网址
    // GOLDEYEGETHOST:'https://goldeye.cfbond.com/',   //接口
};
//global.constants.LOCALHOST
//global.constants.GOLDEYELOCALHOST
//global.constants.GOLDEYEGETHOST

export function GetUrlParam(paraName)  {
　var url = document.location.toString();
    // console.log("url",url)
　var arrObj = url.split("?");
　　　if (arrObj.length > 1) {
　　　　　var arrPara = arrObj[1].split("&");
　　　　　var arr;
　　　　　for (var i = 0; i < arrPara.length; i++) {
　　　　　　　arr = arrPara[i].split("=");
　　　　　　　if (arr != null && arr[0] == paraName) {
　　　　　　　　　return arr[1];
　　　　　　　}
　　　　　}
　　　　　return "";
　　　}
　　　else { return ""; }
}

export function LoginReq (sc) {
        // console.log("secu_code",GetUrlParam("secu_code"))
        if(GetUrlParam("secu_code") === null || GetUrlParam("secu_code") === ""){
            window.location.href = `${global.constants.LOCALHOST}`
        }else{
            //验证TOKEN
            axios.get(`${global.constants.GOLDEYELOCALHOST}cattle/auth_token?token=${GetUrlParam("secu_code")}`)
            .then(function (data){
                console.log("请求验证",data.data.flag);
                if(data.data.flag){
                    //请求基础数据
                    axios.get(`${global.constants.GOLDEYELOCALHOST}cattle/get_company_info?secu_code=${GetUrlParam("secu_code")}`)
                    .then(function (data){
                        // console.log("请求数据",data.data.data);
                        if(data.data.flag){
                            localStorage.setItem("TOKEN",GetUrlParam("secu_code")),
                            localStorage.setItem("secu_code",data.data.data.secu_code),
                            localStorage.setItem("secu_abbr",data.data.data.secu_abbr),
                            localStorage.setItem("company_full_name",data.data.data.company_full_name),
                            localStorage.setItem("market",data.data.data.market)
                        }else{

                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
                }else{

                }
            })
            .catch(function (error) {
                console.log(error);
            });
        }
}
